package com.wechat.page.comps;

import com.swak.fx.support.AbstractComp;
import com.swak.fx.support.AbstractPage;
import com.swak.fx.support.FXMLView;
import com.swak.fx.support.ui.UI;
import com.swak.fx.support.ui.UIAction;
import com.swak.fx.support.ui.UIIcon;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;

/**
 * TitlePage.
 * 
 * @author 618lf
 */
@FXMLView(title = "窗口标题", value = "TitleFixComp.fxml", css = "TitleFixComp.css")
public class TitleFixComp extends AbstractComp {

	protected HBox root;
	protected Label title;
	protected Pane placeholder;

	// 控制按钮
	protected UIAction topBtn;
	protected UIAction minBtn;
	protected UIAction closeBtn;

	public TitleFixComp(AbstractPage parent) {
		super(parent);
		this.root = (HBox) super.root;
		this.topBtn = new UIAction();
		this.minBtn = new UIAction();
		this.closeBtn = new UIAction();
		this.title = new Label("标题");
		this.placeholder = new Pane();
	}

	@Override
	public void initializeParts() {
		super.initializeParts();
		
		this.closeBtn.getStyleClass().add("close-btn");
		this.topBtn.getStyleClass().add("top-btn");
		
		this.topBtn.textNodeProperty().set(new UIIcon(UI.Icons.THUMB_TACK));
		this.minBtn.textNodeProperty().set(new UIIcon(UI.Icons.MINUS));
		this.closeBtn.textNodeProperty().set(new UIIcon(UI.Icons.REMOVE));

		this.title.getStyleClass().add("title");
		this.root.getChildren().addAll(this.title, this.placeholder, this.topBtn, this.minBtn, this.closeBtn);
		HBox.setHgrow(this.placeholder, Priority.ALWAYS);
	}

	@Override
	public void setupEventHandlers() {
		super.setupEventHandlers();
		this.registerUIEvent(this.root, UI.Events.Window_Drag);
		this.registerUIEvent(this.topBtn, UI.Events.Window_Fix);
		this.registerUIEvent(this.minBtn, UI.Events.Window_Min);
		this.registerUIEvent(this.closeBtn, UI.Events.Window_Close);
		this.registerUIEvent(this.root, UI.Events.Window_Max2);
		this.registerUIEvent(this.placeholder, UI.Events.Window_Drag);
	}

	@Override
	public void setupBindings() {
		super.setupBindings();
		this.title.textProperty().bind(this.titleProperty);
	}

	@Override
	public void setupValueChangedListeners() {
		super.setupValueChangedListeners();
		this.fixActiveProperty.addListener((ob, o, n) -> {
			if (n) {
				this.topBtn.getStyleClass().add("active");
			} else {
				this.topBtn.getStyleClass().remove("active");
			}
		});
	}

	@Override
	public void windowStateChange(int state) {
		super.windowStateChange(state);
		this.fixActiveProperty.set((state & UI.States.Window_Fix.index) == UI.States.Window_Fix.index);
	}

	// ================================================================================
	// Properties
	// ================================================================================
	private BooleanProperty fixActiveProperty = new SimpleBooleanProperty(false);
	private StringProperty titleProperty = new SimpleStringProperty();

	public StringProperty titleProperty() {
		return titleProperty;
	}
}
